package com.java110.mall.goods.bmo.housekeepingType.impl;


import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.file.FileDto;
import com.java110.dto.housekeeping.HousekeepingTypeDto;
import com.java110.dto.shop.ShopDto;
import com.java110.mall.goods.bmo.housekeepingType.ISaveHousekeepingTypeBMO;
import com.java110.intf.system.IFileInnerServiceSMO;
import com.java110.intf.goods.IHousekeepingTypeInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.po.housekeeping.HousekeepingTypePo;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("saveHousekeepingTypeBMOImpl")
public class SaveHousekeepingTypeBMOImpl implements ISaveHousekeepingTypeBMO {

    @Autowired
    private IHousekeepingTypeInnerServiceSMO housekeepingTypeInnerServiceSMOImpl;
    @Autowired
    private IFileInnerServiceSMO fileInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    /**
     * 添加小区信息
     *
     * @param housekeepingTypePo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> save(HousekeepingTypePo housekeepingTypePo) {

        if (!StringUtil.isEmpty(housekeepingTypePo.getShopId()) && !"9999".equals(housekeepingTypePo.getShopId())) {
            ShopDto shopDto = new ShopDto();
            shopDto.setShopId(housekeepingTypePo.getShopId());
            shopDto.setState("002");//审核通过的店铺
            List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
            if (ListUtil.isNull(shopDtos)) {
                return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "审核通过店铺才可以添加");
            }
        }

        housekeepingTypePo.setHktId(GenerateCodeFactory.getGeneratorId("11"));

        if (!"9999".equals(housekeepingTypePo.getShopId()) && StringUtil.isEmpty(housekeepingTypePo.getTypeCd())) {
            housekeepingTypePo.setTypeCd(HousekeepingTypeDto.TYPE_CD_HOUSEKEEPING);
        }
        if(!StringUtil.isEmpty(housekeepingTypePo.getHktIcon()) && housekeepingTypePo.getHktIcon().length()> 512) {
            FileDto fileDto = new FileDto();
            fileDto.setContext(housekeepingTypePo.getHktIcon());
            fileDto.setFileName(housekeepingTypePo.getHktId());
            String fileName = fileInnerServiceSMOImpl.saveFile(fileDto);
            housekeepingTypePo.setHktIcon(fileName);
        }
        int flag = housekeepingTypeInnerServiceSMOImpl.saveHousekeepingType(housekeepingTypePo);
        if (flag > 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }

}
